package com.yiren.backstage.judge.services.light.impl;

import com.google.gson.Gson;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.judge.services.light.ILightStateCheckService;

import java.util.Map;
/**
 * @author xiaozan
 * @Description 灯光状态检测  接口
 * @Date 2022/3/25 23:02
 */
public class LightStateCheckServiceImpl implements ILightStateCheckService {

    private CarStateBean carState ;

    private Gson gson;

    /**远近交替次数*/
    private int pdAlternateCount = 0;

    /**远近交替步骤*/
    private int pdAlternateStep = 1;

    /**远近交替是否符合开始状态 远光灯关闭才符合*/
    private boolean pdAlternateStartIsAccord;

    public LightStateCheckServiceImpl(){
        carState= CarStateBean.getInstance();
        gson = new Gson();
    }

    @Override
    public boolean stateIsAccord(String stateMapJson){
        //json转换
        Map<String,Object> stateMap = gson.fromJson(stateMapJson,Map.class);
        //判断状态是否符合
        boolean countResult = true;
        try {
            for (Map.Entry<String,Object> entry : stateMap.entrySet()) {
                countResult = accord(CarStateNameEnum.valueOf(entry.getKey()), entry.getValue());
                if(!countResult) return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return countResult;
    }


    @Override
    public boolean accord(CarStateNameEnum carStateNameEnum,Object value){
        //操作值
        int operactionValue = 0;
        //实时值
        int realTimeValue = 0;
        switch (carStateNameEnum){
            case DRIVING_LAMP://行车灯
                realTimeValue = carState.getDrivingLamp().ordinal();
                break;
            case PASSING_LIGHT:  // 近光
                realTimeValue = carState.getPassingLight().ordinal();
                break;
            case DISTANCE_LIGHT: //远光
                realTimeValue = carState.getDistanceLight().ordinal();
                break;
            case FOGLIGHT:      //雾灯
                realTimeValue = carState.getFoglight().ordinal();
                break;
            case RIGHT_LIGHT:  //右转灯
                realTimeValue = carState.getRightLight().ordinal();
                break;
            case LEFT_LIGHT:  //左转灯
                realTimeValue = carState.getLeftLight().ordinal();
                break;
            case CAUTION_LIGHT: //警示灯
                realTimeValue = carState.getCautionLight().ordinal();
                break;
            case PASSING_OR_DISTANCE_LIGHT: //近光或者远光
                if (carState.getPassingLight() == CarStateValueEnum.OPEN || carState.getDistanceLight() == CarStateValueEnum.OPEN) {
                    realTimeValue = CarStateValueEnum.OPEN.ordinal();
                } else {
                    realTimeValue = CarStateValueEnum.CLOSE.ordinal();
                }
                break;
        }

        //远近交替是检测次数 单独处理
        switch (carStateNameEnum){
            case DISTANCE_LIGHT_ALTERNATE:
                realTimeValue = distanceLightAlternate();
                operactionValue = Integer.parseInt(value.toString());
                break;
            default:
                operactionValue = CarStateValueEnum.valueOf(value.toString()).ordinal();
                break;
        }

        return  realTimeValue == operactionValue ? true: false;
    }


    @Override
    public int distanceLightAlternate(){
        switch (pdAlternateStep){
            case 1:
                //如果远光灯开启先等待远光灯关闭后才检测远近交替
                if(!pdAlternateStartIsAccord){
                    if(carState.getDistanceLight() == CarStateValueEnum.CLOSE) pdAlternateStartIsAccord = true;
                } else{
                    //步骤1 ： 等待远光灯开启
                    if(carState.getDistanceLight() == CarStateValueEnum.OPEN) pdAlternateStep = 2;
                }
                break;
            case 2:
                //步骤2 ：等待远光关闭、近光开启   完成远近交替一次
                if(carState.getDistanceLight() == CarStateValueEnum.CLOSE && carState.getPassingLight() == CarStateValueEnum.OPEN){
                    pdAlternateStep = 1;
                    //累计远近交替次数
                    pdAlternateCount++;
                }
                break;
        }
        return pdAlternateCount;
    }
}
